const R = require('../result/r');
const httpUtil = require('../util/httpUtil')
const dayjs = require('dayjs')
/**
 * 判断当前时间是否在这个时间段内
 */
const isDuringDate = function (beginDate, endDate) {
    let curDate = new Date();
    return curDate >= beginDate && curDate <= endDate;
}

class todayContentController {


    /**
     * 天气接口
     */
    async weather(ctx) {
        const params = ctx.request.body;
        // console.log(params)
        const req = ctx.request;
        let ip = req.headers['x-forwarded-for'] || req.headers['x-real-ip']
        if (ip !== undefined && ip.trim() !== '') {
            params.ip = ip;
        }
        //百度云aip
        let headers = {
            "Content-Type": "application/json;charset=UTF-8",
            "X-Bce-Signature": "AppCode/fd2892a732d8463d9a21e9bf77b97be4",
        }
        let url = `https://gwgp-h4bqkmub7dg.n.bdcloudapi.com/day`;
        await httpUtil.synchronous_request(url, 'GET', params, headers).then(resp => {
            resp = JSON.parse(resp);
            // delete  re
            ctx.body = R.ok().data(resp);
        }).catch(err => {
            ctx.body = R.error(JSON.parse(err))
        });
    }

    /**
     * 星座运势
     */
    async horoscope(ctx) {
        let astro = ctx.query.astro;
        //console.log(astro)
        let params = {
            key: 'fa153051d08c72f2469647415f10a216',
            astro: astro
        }
        //console.log(params);
        await httpUtil.synchronous_request("https://api.tianapi.com/star/index", 'GET', params, null)
            .then(resp => {
                resp = JSON.parse(resp);
                if (resp.code === 200) {
                    ctx.body = R.ok().data(resp.newslist)
                } else {
                    ctx.body = R.error(resp.msg);
                }
            })
            .catch(err => {
                ctx.body = R.ok().data(JSON.parse(err))
            })
    }


    /**
     * 问候语
     */
    async greetings(ctx) {
        let curDate = dayjs().format('YYYY-MM-DD');
        //判断是否位早安时间段
        let isMorning = isDuringDate(dayjs(curDate + " 00:00"), dayjs(curDate + " 10:00"));
        //判断是否为午安时间段
        let isNight = isDuringDate(dayjs(curDate + " 19:00"), dayjs(curDate + " 23:59"));
        let params = {
            key: 'fa153051d08c72f2469647415f10a216',
        }
        let url, title = "问候语";
        if (isMorning) {
            url = 'https://api.tianapi.com/zaoan/index';
        } else if (isNight) {
            url = 'https://api.tianapi.com/wanan/index';
        } else {
            title = '生活小窍门'
            url = 'https://api.tianapi.com/qiaomen/index'
            params.key = 'f4d5d764f8205cf5d688aeb364cc6aec'
        }
        await httpUtil.synchronous_request(url, 'GET', params, null)
            .then(resp => {
                resp = JSON.parse(resp);
                if (resp.code === 200) {
                    resp.newslist[0].title = title;
                    ctx.body = R.ok().data(resp.newslist[0])
                } else {
                    ctx.body = R.error(resp.msg);
                }
            })
            .catch(err => {
                //console.log(err)
                ctx.body = R.ok().data(JSON.parse(err))
            })
    }

}

// 导出 Controller 的实例
module.exports = new todayContentController();
